package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.StartTrainM;
import com.com.ruanmeng.utils.ImgDataUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 锻炼内容列表
 */
public class TrainListActivity extends BaseActivity {

    @BindView(R.id.rlist_trainlist)
    RecyclerView rlistTrainlist;
    @BindView(R.id.refresh_trainlist)
    SwipeRefreshLayout mRefresh;
    private LinearLayout li_null;
    private int page = 1;
    private LinearLayoutManager linearLayoutManager;
    ArrayList<StartTrainM.UserEvalItemsBean> Temp_list = new ArrayList<>();
    ArrayList<StartTrainM.UserEvalItemsBean> Temp_listjz = new ArrayList<>();//脊柱   1
    ArrayList<StartTrainM.UserEvalItemsBean> Temp_listsz = new ArrayList<>();//上肢   2
    ArrayList<StartTrainM.UserEvalItemsBean> Temp_listxz = new ArrayList<>();//下肢   3
    private TrainListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_list);
        ButterKnife.bind(this);
        ChangLayTitle("基础训练");
        LayBack();
        init();
        getData();
    }

    public void init() {
        ///空
        li_null = (LinearLayout) findViewById(R.id.li_trainlist_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(this);
        rlistTrainlist.setLayoutManager(linearLayoutManager);
        mAdapter = new TrainListAdapter(TrainListActivity.this, rlistTrainlist, Temp_list, R.layout.item_trainlist);
        rlistTrainlist.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = sDateFormat.format(new java.util.Date());
                Intent in = new Intent(TrainListActivity.this, TrainContentActivity.class);
                in.putExtra("id", Temp_list.get(position + 1).getGuidePlanId());
                in.putExtra("musiclink", Temp_list.get(position + 1).getGuideMusic());
                in.putExtra("videolink", Temp_list.get(position + 1).getEvalVideo());
                in.putExtra("pic", Temp_list.get(position + 1).getGuidePic());
                in.putExtra("intor", Temp_list.get(position + 1).getEvalText());
                in.putExtra("title", Temp_list.get(position + 1).getEvalItemTip());
                startActivity(in);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }

    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.StartTrain, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(TrainListActivity.this, "uid"));
        mRequest.add("page", page);
        CallServer.getRequestInstance().add(TrainListActivity.this, 0, mRequest,
                new CustomHttpListener(TrainListActivity.this, true, StartTrainM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        StartTrainM model = ((StartTrainM) data);
                        Temp_list.clear();
                        Temp_listjz.clear();
                        Temp_listsz.clear();
                        Temp_listxz.clear();
                        Temp_list.addAll(model.getUserEvalItems());

                        for (int i = 0; i < Temp_list.size(); i++) {
                            if (Temp_list.get(i).getEvalItemType() == 1) {//1脊柱，2上肢，3下肢
                                Temp_listjz.add(Temp_list.get(i));
                            } else if (Temp_list.get(i).getEvalItemType() == 2) {//1脊柱，2上肢，3下肢
                                Temp_listsz.add(Temp_list.get(i));
                            } else if (Temp_list.get(i).getEvalItemType() == 3) {//1脊柱，2上肢，3下肢
                                Temp_listxz.add(Temp_list.get(i));
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (Temp_list.size() == 0) {
                            li_null.setVisibility(View.VISIBLE);
                            rlistTrainlist.setVisibility(View.GONE);
                        } else {
                            li_null.setVisibility(View.GONE);
                            rlistTrainlist.setVisibility(View.VISIBLE);
                        }
                        mRefresh.setRefreshing(false);
                        mAdapter.setLoading(false);
                    }
                }, true, true);
    }

    public class TrainListAdapter extends BaseLoadMoreHeaderAdapter<StartTrainM.UserEvalItemsBean> {


        public TrainListAdapter(Context mContext, RecyclerView recyclerView, List<StartTrainM.UserEvalItemsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, StartTrainM.UserEvalItemsBean s) {
            LinearLayout li_trainlist_type = (LinearLayout) holder.itemView.findViewById(R.id.li_trainlist_type);
            ImageView pic = (ImageView) holder.itemView.findViewById(R.id.imv_train_pic);
            TextView type = (TextView) holder.itemView.findViewById(R.id.tv_trainlist_type);
            TextView title = (TextView) holder.itemView.findViewById(R.id.tv_train_title);
            View view_man = (View) holder.itemView.findViewById(R.id.view_train_man);
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                view_man.setVisibility(View.VISIBLE);
            } else {
                view_man.setVisibility(View.GONE);
            }
            ImgDataUtil.loadImage(TrainListActivity.this, HttpIP.Base_IpIMage + s.getGuidePic(), pic);
//            title.setMovementMethod(LinkMovementMethod.getInstance());
            title.setText(s.getEvalItemTip());
            /**
             * 训练根据评估项目有脊柱训练、上肢训练、下肢训练
             */

            if (Temp_list.size() > 0 && Temp_listjz.size() > 0) {
                if (holder.getLayoutPosition() == 0) {
                    li_trainlist_type.setVisibility(View.VISIBLE);
                    type.setText("脊柱训练");
                } else if (holder.getLayoutPosition() == Temp_listsz.size()) {
                    li_trainlist_type.setVisibility(View.VISIBLE);
                    type.setText("上肢训练");
                } else if (holder.getLayoutPosition() == Temp_listjz.size() + Temp_listsz.size()) {
                    li_trainlist_type.setVisibility(View.VISIBLE);
                    type.setText("下肢训练");
                } else {
                    li_trainlist_type.setVisibility(View.GONE);
                }
            } else if (Temp_list.size() > 0 && Temp_listsz.size() == 0) {
                if (holder.getLayoutPosition() == 0) {
                    li_trainlist_type.setVisibility(View.VISIBLE);
                    type.setText("上肢训练");
                } else if (holder.getLayoutPosition() == Temp_listjz.size() + Temp_listsz.size()) {
                    li_trainlist_type.setVisibility(View.VISIBLE);
                    type.setText("下肢训练");
                } else {
                    li_trainlist_type.setVisibility(View.GONE);
                }
            } else if (Temp_list.size() == 0 && Temp_listxz.size() > 0) {
                if (holder.getLayoutPosition() == 0) {
                    li_trainlist_type.setVisibility(View.VISIBLE);
                    type.setText("下肢训练");
                } else {
                    li_trainlist_type.setVisibility(View.GONE);
                }
            } else {
                li_trainlist_type.setVisibility(View.GONE);
            }
        }
    }
}
